using System;
using System.Diagnostics;

using Evaluant.Uss;
using Evaluant.Uss.Common;
using Evaluant.Uss.Commands;

namespace Evaluant.Uss.Trace
{
	/// <summary>
	/// Visitor pattern to execute concrete commands
	/// </summary>
	class TraceCommandProcessor : ICommandProcessor
	{

		private string _NewId;
		public string NewId
		{
			get { return _NewId; }
			set { _NewId = value; }
		}

		public void Process(Command c)
		{
			System.Diagnostics.Trace.Write("Command");
		}

		public void Process(CreateEntityCommand c)
		{
			WriteEntityCommand("Create", c);
		}

		public void Process(DeleteEntityCommand c)
		{
			WriteEntityCommand("Delete", c);
		}

		public void Process(CreateAttributeCommand c)
		{
			WriteAttributeCommand("Create", c);
		}

		public void Process(CompoundCreateCommand c)
		{
			Process((CreateEntityCommand)c);
			foreach(CreateAttributeCommand cc in c.InnerCommands)
				Process(cc);
		}

		public void Process(CompoundUpdateCommand c)
		{
			foreach(Command cc in c.InnerCommands)
				cc.Accept(this);
		}

		public void Process(UpdateAttributeCommand c)
		{
			WriteAttributeCommand("Update", c);
		}

		public void Process(DeleteAttributeCommand c)
		{
			WriteAttributeCommand("Delete", c);
		}

		public void Process(CreateReferenceCommand c)
		{
			WriteReferenceCommand("Create", c);
		}

		public void Process(DeleteReferenceCommand c)
		{
			WriteReferenceCommand("Delete", c);
		}

		protected void WriteEntityCommand(string prefix, EntityCommand c)
		{
			System.Diagnostics.Trace.WriteLine(String.Format("{1} Type:{0} Parent:{2} ", c.Type, prefix, c.ParentId));
		}

		protected void WriteReferenceCommand(string prefix, ReferenceCommand c)
		{
			System.Diagnostics.Trace.WriteLine(String.Format("{1} Role:{0} Parent:{2}", c.Role, prefix, c.ParentId));
		}

		protected void WriteAttributeCommand(string prefix, AttributeCommand c)
		{
			System.Diagnostics.Trace.WriteLine(String.Format("{2} Name:{0} Value:{1} Parent:{3}", c.Name, c.Value, prefix, c.ParentId));
		}

	}
}
